package cn.edu.nenu.control.dto;


import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;

import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import cn.edu.nenu.contract.dto.EmployeeDTO;
import cn.edu.nenu.control.SysControl;
import cn.edu.nenu.model.Employee;


@Stateless
public class SysControlBean implements SysControl {
	@PersistenceContext(name = "ITaJPA")
	private EntityManager em;

	@Override
	public String sayHello(String name) {
		return "Hello, " + name;
	}

	@SuppressWarnings("unchecked")
	@Override
	public EmployeeDTO login(String username, String password) {

		Query query = em.createQuery("select e from Employee e where e.employeeusername="+username+" AND e.employeepassword="+password);
		List<Employee> emp =  query.getResultList(); ; 
		
	    if (emp.toString()!="[]") {
						
//			System.out.println(CreateEmployeedto(emp.get(0)));

			return  CreateEmployeedto(emp.get(0));
		}
	else {
		
		return null;}
	}

	private static EmployeeDTO CreateEmployeedto(Employee emp) {
	
		EmployeeDTO empdto = new EmployeeDTO(
				   emp.getEmployeeName(),
				   emp.getEmployeeEmail(),
				   emp.getEmployeeUsername(),
				   emp.getEmployeePassword(),
				   emp.getEmployeeType(),
				   emp.getEmployeeId()
				);
		return empdto;
	
	}

	

	@Override
	public void logout() {
		// TODO Auto-generated method stub
		
	}
}
